package cn.supermay.tool.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
/**
 * 弹出对话框，根据时间自动关闭
 * @author lijintao
 *
 */
public class TimerCloseFrame extends JFrame {

	private static final long serialVersionUID = 8788425229353948570L;

	private static List<TimerCloseFrame> tips = new ArrayList<TimerCloseFrame>();

	private String title;
	private Object message;
	private long millisecond;
	private boolean isHide = true;

	/** Creates new form TimerCloseFrame */
	public TimerCloseFrame(Object message) {
		this("提示", message, 3000);
	}

	public TimerCloseFrame(String title, Object message) {
		this(title, message, 3000);
	}

	public TimerCloseFrame(Object message, long millisecond) {
		this("提示", message, millisecond);
	}

	public TimerCloseFrame(String title, Object message, long millisecond) {
		this.title = title;
		this.millisecond = millisecond;
		this.message = message != null ? message.toString().replaceAll("\n", "<br/>") : "";
		initComponents();
		init();
		tips.add(this);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		mainPanel = new javax.swing.JPanel();
		messageLabel = new javax.swing.JLabel();

		setAlwaysOnTop(true);
		setMinimumSize(new java.awt.Dimension(320, 180));

		messageLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);

		javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
		mainPanel.setLayout(mainPanelLayout);
		mainPanelLayout
				.setHorizontalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGap(0, 291, Short.MAX_VALUE)
						.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(mainPanelLayout.createSequentialGroup()
										.addGap(1, 1, 1).addComponent(messageLabel,
												javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
										.addGap(1, 1, 1))));
		mainPanelLayout
				.setVerticalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGap(0, 141, Short.MAX_VALUE)
						.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(mainPanelLayout.createSequentialGroup()
										.addGap(1, 1, 1).addComponent(messageLabel,
												javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
										.addGap(1, 1, 1))));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mainPanel,
						javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mainPanel,
						javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JPanel mainPanel;
	private javax.swing.JLabel messageLabel;

	// End of variables declaration//GEN-END:variables

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new TimerCloseFrame(
						"你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1你好啊啊1",
						5000).setVisible(true);
				new TimerCloseFrame("你好啊啊2", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊3", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊4", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊4", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊4", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊4", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊5", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊6", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊7", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊8", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊9", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊10", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊11", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊12", 5000).setVisible(true);
				new TimerCloseFrame("你好啊啊13", 5000).setVisible(true);
			}
		});
	}

	public void init() {
		setPreferredSize(new java.awt.Dimension(320, 180));
		setSize(new java.awt.Dimension(320, 180));
		messageLabel.setBackground(Color.yellow);
		messageLabel.setPreferredSize(new java.awt.Dimension(320, 160));
		setTitle(title);
		messageLabel.setText("<html>" + message + "</html>");
		// 下面这句代码使得JFrame 透明， 但是，1.7的JDK中有问题，
		// com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.9f);
		java.awt.Toolkit.getDefaultToolkit().beep();
		int x = this.getToolkit().getScreenSize().width - this.getWidth() - tips.size() / 4 * 320;
		int y = this.getToolkit().getScreenSize().height - this.getHeight()
				- Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom
				- (tips.size() % 4) * 180;
		this.setLocation(x, y);
		this.addMouseListener(mouseAdapter);
		this.mainPanel.addMouseListener(mouseAdapter);
		this.messageLabel.addMouseListener(mouseAdapter);
		this.messageLabel.addMouseMotionListener(mouseAdapter);
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				millisecond = 0;
				toHide(flag);
			}

		});
		new Thread(new HideRunnable(flag)).start();
	}

	private MouseAdapter mouseAdapter = new MouseAdapter() {

		@Override
		public void mouseEntered(MouseEvent e) {
			isHide = false;
		}

		@Override
		public void mouseExited(MouseEvent e) {
			flag++;
			isHide = true;
			new Thread(new HideRunnable(flag)).start();
		}

	};

	private int flag = 1;

	class HideRunnable implements Runnable {

		private int i;

		public HideRunnable(int i) {
			this.i = i;
		}

		@Override
		public void run() {
			try {
				Thread.sleep(millisecond);
				toHide(i);
			} catch (InterruptedException e) {
			}
		}
	}

	public void toHide(int i) {
		if (isHide && this.flag == i) {
			this.setVisible(false);
			tips.remove(this);
			this.dispose();
		}
	}

	public void setMessage(Object message) {
		this.message = message != null ? message.toString().replaceAll("\n", "<br/>") : "";
		messageLabel.setText("<html>" + message + "</html>");
	}

}
